﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using System.Collections.ObjectModel;

namespace eLearning.DosenView
{
    public partial class TugasMhsPage : PhoneApplicationPage
    {
        private Tugas tugas;

        public TugasMhsPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DataContextPage_Loaded);
        }

        private void DataContextPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(Config.TUGAS))
            {
                tugas = (Tugas)PhoneApplicationService.Current.State[Config.TUGAS];
            }
            LoadTugasMhs();            
        }

        private void LoadTugasMhs()
        {
            pivotTugas.SelectedIndex = 0;
            listMhs1.ItemsSource = null;

            string url = string.Format("{0}&{1}", Config.Instance.TUGAS_SERVICE_URL() + Config.VIEW_DETAIL_KEY, Guid.NewGuid().ToString());
            string json = JsonConvert.SerializeObject(tugas);

            Uri uri = new Uri(url, UriKind.Absolute);
            WebClient webClient = new WebClient();

            webClient.Encoding = Encoding.UTF8;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
            webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listTugas_UploadStringCompleted);
            try
            {
                webClient.UploadStringAsync(uri, "POST", json);
            }
            catch (Exception) { }
        }

        private void listTugas_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            try
            {
                ObservableCollection<TugasDetail> list = JsonConvert.DeserializeObject<ObservableCollection<TugasDetail>>(e.Result);
                if (list.Count > 0)
                {
                    listMhs1.ItemsSource = list;
                }
                else
                {
                    listMhs1.ItemsSource = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listTugas_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                TugasDetail detail = (TugasDetail)e.AddedItems[0];
                ((ListBox)sender).SelectedIndex = -1;

                if (detail != null)
                {
                    try
                    {
                        var task = new Microsoft.Phone.Tasks.WebBrowserTask
                        {
                            Uri = new Uri(detail.Link.Contains("http://") ? detail.Link : "http://" + detail.Link)
                        };

                        task.Show();
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
            }
        }

        private void pivotTugas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            if (pivotTugas.SelectedIndex != 0)
                pivotTugas.SelectedIndex = 0;
            else
                NavigationService.GoBack();
        }

    }
}